package de.islanders.unit.player;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import de.islanders.exceptions.OverEncumberedException;
import de.islanders.gameobject.GameObject;

public class Inventory {
	private double  currentEncumbrance,
					maxEncumbrance;
	private List<GameObject> items;
	
	public Inventory(){
		items = new LinkedList<GameObject>();
	}

	/**
	 * @return the currentEncumbrance
	 */
	public double getCurrentEncumbrance() {
		return currentEncumbrance;
	}

	/**
	 * @param currentEncumbrance the currentEncumbrance to set
	 */
	public void setCurrentEncumbrance(double currentEncumbrance) {
		this.currentEncumbrance = currentEncumbrance;
	}

	/**
	 * @return the maxEncumbrance
	 */
	public double getMaxEncumbrance() {
		return maxEncumbrance;
	}

	/**
	 * @param maxEncumbrance the maxEncumbrance to set
	 */
	public void setMaxEncumbrance(double maxEncumbrance) {
		this.maxEncumbrance = maxEncumbrance;
	}
	
	/**
	 * @return the items
	 */
	public List<GameObject> getItems() {
		return items;
	}

	/**
	 * @param items the items to set
	 */
	public void setItems(List<GameObject> items) {
		this.items = items;
	}

	/**
	 * @param item the item to add to the inventory
	 */
	public void add(GameObject item){
		items.add(item);
	}
	
	/**
	 * TODO find a realistic formula for maxEncumbrance
	 * TODO observer pattern for verification?
	 * @param player
	 */
	public void verifyEncumbrance(Player player){
		int fatigue = player.getStatus().getFatigue();
		List<GameObject> inventoryBuffer;
		
		maxEncumbrance = fatigue;
		inventoryBuffer = new LinkedList<GameObject>(items);
		inventoryBuffer.addAll(Arrays.asList(player.getEquip().getEquipSlots()));
		
		for(GameObject item : inventoryBuffer){
			if(item.getWeight() != GameObject.WEIGHT_INFINITE){
				currentEncumbrance += item.getWeight();
			} else {
				throw new OverEncumberedException();
			}
		}
	}
	
	/**
	 * @return int the encumbrance of the player in percent
	 */
	public int getEncumbranceInPercent(){
		return (int) (currentEncumbrance / maxEncumbrance * 100);
	}
}
